file_name=$1
file=$file_name

lang="eng"

LEN=$(echo ${#file_name})

if [ $LEN -eq 0 ]; then
   echo "Nenhum Arquivo encontrado (parametros)"
   exit
fi


gtext=""
#===================================================================
action_process_img() 
#===================================================================
# params ($1=level1 $2=level2 $3=level blur $4=black-threshold"34000")
{
	
	convert $file -crop 180x38+0+8 $file.png
	
	
	#Altera os níveis de cores
	convert $file.png -level $1,0,$2 $file.png
	
	if [ $3 -eq "1" ]; then
		convert $file.png -colorspace Gray -gaussian-blur 9.0 -level 0,60%,0,1 -paint 1 -depth 1 -quality 100 $file.png
	fi

	#Converte para padrão de imagem com cores reduzidas
	convert  $file.png  $file.pgm

	#Transforma os pixels de uma determinada frequencia em pretos
	convert  $file.pgm -black-threshold 65000  $file.png

	#Desfoca a imagem e transforma os pixels acinzentados em pretos
	convert $file.png -blur 1 -black-threshold 65000 +compress $file.png

	if [ $4 -eq "1" ]; then
		#Transforma os pixels de uma determinada frequencia em pretos
		convert  $file.png -black-threshold 34000  $file.png
		#Desfoca a imagem e transforma os pixels acinzentados em pretos
		convert $file.png -blur 1 -black-threshold 34000 +compress $file.png	 
	fi

	#Inverte as cores da imagem
	convert  $file.png -negate  $file.png

	#Converte a imagem para um padrão de cores reconhecido pelo tesseract
	convert $file.png -colors 256 -alpha off -density 300 $file.png

	convert  $file.png $file.tif
   	
	tesseract $file.tif $file -l $lang
	text=`cat $file.txt`
	text="`echo $text | sed 's/[^A-Za-z0-9]//g'`"
	text="`echo $text | sed 's/[ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ]//g'`"
text="`echo $text | sed 's/[ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿğ]//g'`"
	gtext=$text
	#echo $gtext
	echo "$gtext" > $file.txt
}


############## PERCORRE TODOS OS JPG E PROCESSA ###################
#for file in `find . -name "$file_name"`
#do
	echo "Processando $file …"
	###############################################################
	## FAZ VARIOS PROCESSAMENTOS PARA ENCONTRAR A IMAGEM CORRETA ##
	###############################################################
	action_process_img "20000" "20000" "0" "1"
	if [ $(echo ${#gtext}) -ne 6 ]; then

		action_process_img "20000" "20000" "0" "0"
		if [ $(echo ${#gtext}) -ne 6 ]; then
			#Tenta com level diferenciado
			action_process_img "10000" "40000" "0" "0"

			if [ $(echo ${#gtext}) -ne 6 ]; then
				#tenta com level 10000
				action_process_img "10000" "10000" "0" "0"

				if [ $(echo ${#gtext}) -ne 6 ];  then
					#tenta colocan do level 30000
					action_process_img "30000" "30000" "0" "0"
				
					if [ $(echo ${#gtext}) -ne 6 ]; then
						#Tenta com level 40000
						action_process_img "40000" "40000" "0" "0"

					
						if [ $(echo ${#gtext}) -ne 6 ]; then
							#Tenta colocando blur
							action_process_img "20000" "20000" "1" "0"
							if [ $(echo ${#gtext}) -ne 6 ]; then
								#Tenta colocando black-threshold
								action_process_img "20000" "20000" "1" "1"
							fi
						fi	
					fi

				fi
			fi
		fi
	fi

	echo "Text: $gtext"

	rm -f $file.tif
	rm -f $file.pgm
	rm -f $file.png

	###########################
#done





